'use strict';

const Service = require('egg').Service;

class BookService extends Service {
//新建图书
    async create(payload) {
        console.log(payload);
           const res= await this.ctx.model.Book.create(payload)
        return res
    }

//图书展示payloadpayload
    async show(){
        const res=await this.ctx.model.Book.find();
        return(this.ctx.body=res);
    }
//书名查找
    async findname(_id){
        console.log(_id);
        const res=await this.ctx.model.Book.findById(_id);
        // const res=await this.ctx.model.Book.find();
        return (this.ctx.body=res);
    }
//兴趣查找
    async findintrest(kind){
        console.log(kind);
        const res=await this.ctx.model.Book.find({kind:{$regex:kind.kind}});
        // const res=await this.ctx.model.Book.find();
        return (this.ctx.body=res);
    }
    //
    async delete(_id){
        const res=await this.ctx.model.Book.findByIdAndDelete(_id)
        return res;
    }
    //
    async update(_id,payload){
        const res=await this.ctx.model.Book.findByIdAndUpdate(_id,payload)
        return res;
    }
    //借阅图书
    async borrow(_id){
        const res=await this.ctx.model.Book.updateOne(_id,{"$inc":{"num":-1}})
        return res;
    }
    async return(bookname){
        const res=await this.ctx.model.Book.updateOne(bookname,{"$inc":{"num":1}})
        return res;
    }
}

module.exports = BookService;
